package com.jsgt.greekfire.ui.me.aboutus

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.AppConfig
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.Version
import com.jsgt.greekfire.ui.dialog.DialogUtils
import com.jsgt.greekfire.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_about_us.*

class AboutUsActivity : BaseActivity(), View.OnClickListener {
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(App.context).get(UserViewModel::class.java)
    }

    private var version: Version? = null
    private var showUploadDialog = false
    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int = R.layout.activity_about_us

    @SuppressLint("SetTextI18n")
    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        title = getString(R.string.about_us)
        tvVersionNumber.text = "v" + AppConfig.getVersion()
        rllVersionUpdate.setOnClickListener(this)
        itemWebsite.setOnClickListener(this)
        itemForum.setOnClickListener(this)
        itemTwitter.setOnClickListener(this)

        userViewModel.versionLiveData.observe(this, Observer {
            version = it
            if (AppConfig.getVersion()!! < version?.version!!) {
                tvNew.visibility = View.VISIBLE
            }
            showProgress(false)
            if (showUploadDialog) {
                DialogUtils.showUploadDialog(this, version!!)
            }
        })
        userViewModel.errorLiveData.observe(this, Observer {
            showProgress(false)
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rllVersionUpdate -> {
                showUploadDialog = true
                showProgress(true)
                userViewModel.getVersion()
            }
            R.id.itemWebsite -> {
                ToastUtils.show(R.string.not_yet_open)
            }
            R.id.itemForum -> {
                ToastUtils.show(R.string.not_yet_open)
            }
            R.id.itemTwitter -> {
                ToastUtils.show(R.string.not_yet_open)
            }
        }
    }


}